package com.lai.note.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.lai.note.R;
import com.lai.note.dto.ResponseResult;
import com.lai.note.model.User;
import com.lai.note.service.UserBaseService;
import com.lai.note.util.GsonUtil;
import com.lai.note.util.RetrofitRequest;
import com.lai.note.util.ToastUtil;
import com.lai.note.util.TokenUtil;
import com.lai.note.util.UserBaseUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class LoginActivity extends AppCompatActivity {

    //登录相关信息及控件
    private EditText et_phone;
    private EditText et_pwd;
    private Button btn_login;
    private TextView tv_toRegister;
    private TextView tv_toRetrive;

    private String phone;
    private String pwd;

    private Retrofit retrofit;
    private UserBaseService userBaseService;

    //要跳转的intent
    private Intent intent = null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        setListeners();
        Intent intent = getIntent();
        System.out.println("intent:-->"+intent);
        if(intent!=null){
            et_phone.setText(intent.getStringExtra("phone"));
            et_pwd.setText(intent.getStringExtra("pwd"));
        }
    }


    private void initViews(){
        et_phone = findViewById(R.id.et_userPhone);
        et_pwd = findViewById(R.id.et_password);
        btn_login = findViewById(R.id.btn_login);
        tv_toRegister = findViewById(R.id.tv_toRegister);
        tv_toRetrive = findViewById(R.id.tv_toRetrieve);
    }

    private void setListeners(){
        OnClick onclick = new OnClick();
        btn_login.setOnClickListener(onclick);
        tv_toRegister.setOnClickListener(onclick);
        tv_toRetrive.setOnClickListener(onclick);
    }

    private class OnClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn_login:
                    phone = et_phone.getText().toString();
                    pwd = et_pwd.getText().toString();
                    retrofit = RetrofitRequest.getInstance(v.getContext());
                    userBaseService = retrofit.create(UserBaseService.class);
                    Call<ResponseResult> call = userBaseService.loginPwd(phone, pwd);
                    call.enqueue(new Callback<ResponseResult>() {
                        @Override
                        public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                            System.out.println("response:-->"+response);
                            ResponseResult res = response.body();
                            Object data = res.getData();
                            String userJson = GsonUtil.object2Json(data);
                            User user = new Gson().fromJson(userJson, User.class);
                            System.out.println("res:-->"+res);
                            if(res.getStatus()==200){
                                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                                startActivity(intent);
                                UserBaseUtil.ISLOGINED=true;
                                System.out.println("此时登录状态:-->"+(UserBaseUtil.ISLOGINED?"已登录":"未登录"));
                                TokenUtil.getToken(getApplicationContext());
                            }else{
                                ToastUtil.display(getApplicationContext(),res.getMessage());
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseResult> call, Throwable t) {
                            t.printStackTrace();
                        }
                    });
                    break;
                case R.id.tv_toRegister:
                    intent = new Intent(LoginActivity.this,RegisterActivity.class);
                    startActivity(intent);
                    break;
                case R.id.tv_toRetrieve:
                    intent = new Intent(LoginActivity.this,RetrieveActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    }



}
